<?php

/**
 * This is the model class for table "{{profession}}".
 *
 * The followings are the available columns in table '{{profession}}':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property string $mobile
 * @property string $email
 * @property integer $professionType_id
 * @property integer $country_id
 * @property integer $state_id
 * @property integer $region_id
 * @property double $lat
 * @property double $long
 * @property integer $avgRate
 * @property integer $created_by
 * @property string $created_date
 *
 * The followings are the available model relations:
 * @property User $createdBy
 * @property Country $country
 * @property ProfessionType $professionType
 * @property State $state
 * @property Region $region
 * @property Rate[] $rates
 * @property Comment[] $comments
 * @property State $state
 * @property User $createdBy
 */
class Profession extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{profession}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('mobile, created_by', 'numerical', 'integerOnly'=>true),
			array( 'name , professionType_id, country_id, state_id, region_id, created_by, created_date', 'required','message'=>Yii::t('default','required.{attribute}')),
			array('lat, long', 'numerical'),
			//array('lat', 'required','message'=>Yii::t('profession_messages','required.coords')),
			array('name, email', 'length', 'max'=>256),
			array('description', 'length', 'max'=>2048),
			array('created_date', 'safe'),
			array('email','email','message'=>Yii::t('default','email.{attribute}')),	
// 			array('state_id','compare','compareValue'=>0,'operator'=>'!=', 'allowEmpty'=>false , 'message'=>'{attribute} enter.'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, description, mobile, email, professionType_id, country_id, state_id, region_id, lat, long, created_by, created_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'createdBy' => array(self::BELONGS_TO, 'User', 'created_by'),
			'country' => array(self::BELONGS_TO, 'Country', 'country_id'),
			'professionType' => array(self::BELONGS_TO, 'ProfessionType', 'professionType_id'),
			'state' => array(self::BELONGS_TO, 'State', 'state_id'),
			'region' => array(self::BELONGS_TO,'Region', 'region_id'),
			'rates' => array(self::HAS_MANY, 'Rate', 'professionid'),
			'ratesCount' => array(self::STAT, 'Rate','professionid'),
			'comments' => array(self::HAS_MANY, 'Comment', 'professionid'),
			'commentCount' => array(self::STAT, 'Comment', 'professionid', 'condition'=>'status='.Comment::STATUS_APPROVED),
				
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				
			'name' => Yii::t('profession_messages', 'profession.name'),
			'description' => Yii::t('profession_messages', 'profession.description'),
			'mobile' => Yii::t('profession_messages', 'profession.mobile'),
			'email' => Yii::t('profession_messages', 'profession.email'),
			'professionType_id' => Yii::t('profession_messages', 'profession.professionType_id'),
			'state_id' => Yii::t('profession_messages', 'profession.state_id'),
			'region_id' => Yii::t('profession_messages', 'profession.region_id'),
			'created_by'=>Yii::t('profession_messages', 'profession.created_by'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('professionType_id',$this->professionType_id);
		$criteria->compare('country_id',$this->country_id);
		$criteria->compare('state_id',$this->state_id);
		$criteria->compare('region_id',$this->region_id);
		$criteria->compare('lat',$this->lat);
		$criteria->compare('long',$this->long);
		$criteria->compare('avgRate',$this->avgRate);
		if(Yii::app()->user->role!='admin'){
				$criteria->compare('created_by',Yii::app()->user->id);
		}
		$criteria->compare('created_date',$this->created_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function getProfessionTypeString()
	{
		return Yii::t('professionType_messages', 'type.'.$this->professionType->name);
	}
	
	public function getStateString()
	{
		return Yii::t('state_messages', 'state.'.$this->state->name);
	}
	
	public function getRegionString()
	{
		return Yii::t('region_messages', 'region.'.$this->region->name);
	}
	
	public function getUserRate(){
		$rateObj=Rate::model()->find('professionid=:professionId and userid=:userid', array(':professionId'=>$this->id,':userid'=>Yii::app()->user->id));
		if($rateObj != null)
		return $rateObj->rate;
		else return null;
	}

	
	public function addComment($comment)
	{
		if(Yii::app()->params['commentNeedApproval'])
			$comment->status=Comment::STATUS_PENDING;
		else
			$comment->status=Comment::STATUS_APPROVED;
		$comment->professionid=$this->id;
		$comment->userid =Yii::app()->user->id;
		$comment->created_date =  time();
		$comment->url = "ok";
		return $comment->save();
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Profession the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
